<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
          >删 除
        </el-button>
      </template>
      <template slot="taxNoForm">
        <div style="display: flex; align-items: center">
          <el-input
            v-model="form.taxNo"
            disabled
            placeholder="请输商品编号"
          />
          <el-button
            type="text"
            size="mini"
            @click="handleSelect"
            style="margin-left: 10px"
            >查找</el-button
          >
        </div>
      </template>
    </avue-crud>
    <el-dialog
      class="el-dialog__wrapper avue-dialog avue-crud__dialog"
      title="商品信息"
      :visible.sync="dialogVisible"
      append-to-body
      :close-on-click-modal="false"
      :before-close="handleClose"
      width="70%"
    >
      <el-row>
        <el-col :span="5">
          <div class="box">
            <el-scrollbar>
              <basic-container>
                <avue-tree
                  :option="treeOption"
                  :data="treeData"
                  @node-click="nodeClick"
                />
              </basic-container>
            </el-scrollbar>
          </div>
        </el-col>
        <el-col :span="19">
          <basic-container>
            <avue-crud
              :option="goodsOption"
              :search.sync="goodsSearch"
              :data="goodsData"
              ref="crud"
              v-model="goodsForm"
              :page.sync="goodsPage"
              @search-change="goodsSearchChange"
              @search-reset="goodsSearchReset"
              @current-change="goodsCurrentChange"
              @size-change="goodsSizeChange"
              @refresh-change="goodsRefreshChange"
              @on-load="goodsOnLoad"
            >
              <template slot="menu" slot-scope="{ row }">
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-check"
                  @click="handleCheck(row)"
                  >选择</el-button
                >
              </template>
            </avue-crud>
          </basic-container>
        </el-col>
      </el-row>
      <!-- <div slot="footer" class="avue-dialog__footer avue-dialog__footer--right">
        <el-button type="primary" @click="goodsSubmit()">确 定</el-button>
      </div> -->
    </el-dialog>
  </basic-container>
</template>
  
  <script>
import {
  getList,
  getDetail,
  save,
  remove,
} from "@/api/rb-oem/payinvoice/payinvoiceGoods";
import { getLazyList } from "@/api/rb-oem/taxCategory/tax_category";
import { getList as getGoodsList } from "@/api/rb-oem/taxCategory/tax_product";
import { mapGetters } from "vuex";

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      search: {},
      active: 0,
      openType: "", //打开dialog弹框的类型
      roleId: null, //角色id
      userType: "3",
      props: {
        //菜单权限数据 props
        label: "title",
        value: "key",
        disabled: this.isDisabledFn,
      },
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        labelWidth: "150",
        columnBtn: false,
        addBtn: true,
        editBtn: true,
        viewBtn: true,
        delBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "项目名称",
            prop: "itemName",
            type: "input",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入项目名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商品编号",
            prop: "taxNo",
            type: "number",
            minWidth: 150,
            disabled: true,
            rules: [
              {
                required: true,
                message: "请输入商品编号",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商品名称",
            prop: "productName",
            type: "input",
            minWidth: 150,
            disabled: true,
            rules: [
              {
                required: true,
                message: "请输入商品名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "税率(%)",
            prop: "taxRate",
            type: "number",
            minWidth: 150,
            disabled: true,
            rules: [
              {
                required: true,
                message: "请输入税率编码",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商品简称",
            prop: "shortName",
            type: "input",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入商品简称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "含税单价",
            prop: "price",
            type: "number",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入含税单价",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "含税金额",
            prop: "amount",
            type: "number",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入含税金额",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商品单位",
            prop: "unit",
            type: "input",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入商品单位",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商品规格",
            prop: "specification",
            type: "input",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请输入商品规格",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "优惠政策标识",
            prop: "discountPolicy",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            // search: true,
            dicUrl:
              "/api/blade-system/dict/dictionary?code=discount_policy_type",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请选择优惠政策标识",
                trigger: "blur",
              },
            ],
          },
          {
            label: "是否默认",
            prop: "isDefault",
            type: "radio",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            value: "1",
            dicData: [
              {
                dictKey: 0,
                dictValue: "否",
              },
              {
                dictKey: 1,
                dictValue: "是",
              },
            ],
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "是否默认",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
      dialogVisible: false,
      treeData: [],
      treeOption: {
        nodeKey: "id",
        lazy: true,
        treeLoad: function (node, resolve) {
          const parentCategoryNo = node.level === 0 ? 0 : node.data.categoryNo;
          getLazyList(parentCategoryNo).then((res) => {
            resolve(
              res.data.data.map((item) => {
                return {
                  ...item,
                  leaf: !item.hasChildren,
                };
              })
            );
          });
        },
        addBtn: false,
        menu: false,
        size: "small",
        props: {
          labelText: "标题",
          label: "categoryName",
          value: "categoryNo",
          children: "children",
        },
      },
      parentCategoryNo: 0,
      goodsSearch: {},
      goodsData: [],
      goodsForm: {},
      goodsPage: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      goodsOption: {
        height: "auto",
        calcHeight: 80,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        display: true,
        // dialogType: 'drawer',
        dialogClickModal: false,
        column: [
          {
            label: "分类名称",
            prop: "categoryName",
            type: "input",
            display: false,
            rules: [
              {
                required: true,
                message: "请输入分类名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "商品编码",
            prop: "taxNo",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入商品编码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "商品名称",
            prop: "taxProductName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入商品名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "税率(%)",
            prop: "taxRate",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入税率",
                trigger: "blur",
              },
            ],
          },
          {
            label: "简称",
            prop: "taxShortName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入简称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "关键字",
            prop: "taxKeyName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入关键字",
                trigger: "blur",
              },
            ],
          },
          {
            label: "描述",
            prop: "description",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入描述",
                trigger: "blur",
              },
            ],
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission", "selectMer"]),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {},
  methods: {
    rowSave(row, done, loading) {
      save({ ...row, rpiNo: this.$route.query.rpiNo }).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      save({ ...row, rpiNo: this.$route.query.rpiNo }).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    handleClose(done) {
      done();
    },
    beforeOpen(done, type) {
      this.openType = type;
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    /**
     * @func 菜单权限树形是否禁止
     * @desc dialog打开类型为 view 则禁止权限树
     * @param {String} openType - 打开类型
     * */
    isDisabledFn() {
      const _this = this;
      if (["view"].includes(_this.openType)) {
        return true;
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    nodeClick(data) {
      this.parentCategoryNo = data.categoryNo;
      this.goodsPage.currentPage = 1;
      this.goodsOnLoad(this.goodsPage);
    },
    handleSelect() {
      this.dialogVisible = true;
    },
    handleCheck(row) {
      this.form.productName = row.taxProductName;
      this.form.taxNo = row.taxNo;
      this.form.taxRate = row.taxRate;
      this.dialogVisible = false;
    },
    goodsSearchChange(params, done) {
      this.goodsQuery = params;
      this.goodsPage.currentPage = 1;
      this.goodsOnLoad(this.goodsPage, params);
      done();
    },
    goodsSearchReset() {
      this.goodsQuery = {};
      this.goodsOnLoad(this.goodsPage);
    },
    goodsCurrentChange(currentPage) {
      this.goodsPage.currentPage = currentPage;
    },
    goodsSizeChange(pageSize) {
      this.goodsPage.pageSize = pageSize;
    },
    goodsRefreshChange() {
      this.goodsOnLoad(this.goodsPage, this.goodsQuery);
    },
    goodsOnLoad(page, params = {}) {
      getGoodsList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.goodsQuery),
        this.parentCategoryNo
      ).then((res) => {
        const data = res.data.data;
        this.goodsPage.total = data.total;
        this.goodsData = data.records;
      });
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, { rpiNo: this.$route.query.rpiNo })
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>
  
  <style>
</style>
  